<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style></style>
<body>
		
</body>
<script>
	
	// 递归和尾递归
	function factorial1(n) {
		if (n === 1) return 1
		return n * factorial1(n - 1)
	}
	factorial1(5) // 120

	function factorial2(n, total) {
		if (n === 1) return total
		return factorial2(n - 1, n * total)
	}
	factorial2(5, 1) // 120

	// 数组求和
	function sumArray(arr, total) {
		if (arr.length === 1) {
			return total
		}
		return sumArray(arr, total + arr.pop())
	}
	let arr = [1, 2, 3, 4]
	sumArray(arr, arr[0]) // 10

	// 斐波那且数列
	function factorial3(n, start = 1, total = 1) {
		if (n <= 2) {
			return total
		}
		return factorial3(n - 1, total, total + start)
	}
	factorial3(9) // 34

</script>
</html>